<?php
/**
 * @package		简易CMS
 * @subpackage	菜单管理模块
 * @version		$Id: admincp_nav.php 23 2012-06-11 18:04:03Z htmambo@gmail.com $
 * @author		Hoping
 * @copyright	Copyright (C) 2010 Hoping Software Studio.
 */

if(!defined('IN_JYCMS') || !defined('IN_ADMINCP')) {
	exit('Access Denied');
}

cpheader();

$navs = array('headernav', 'footernav');
$navdata = array();
foreach((array)$navs as $nav) {
	$navdata[] = array('nav_nav_'.$nav, 'nav&operation='.$nav, $nav == $operation);
}

if($operation == 'headernav') {

	if(!$do) {

		if(!submitcheck('submit')) {

			shownav('style', 'nav_setting_customnav');
			showsubmenu('nav_setting_customnav', $navdata);

			showformheader('nav&operation=headernav');
			showtableheader();
			showsubtitle(array('', 'display_order', 'name', 'url', 'type', 'available', ''));

			$navlist = $subnavlist = array();
			require_once libfile('class/tree');
			$tree = new tree();
			$query = DB::query("SELECT * FROM #__common_nav WHERE navtype='0' ORDER BY displayorder");
			while($nav = DB::fetch($query)) {
				$tree->setNode($nav['id'], $nav['parentid'], $nav);
				$navlist[] = $nav;
			}
			$navtree = $tree->buildTree();
			if($navtree){
				foreach((array)$navtree as $nav){
					showtablerow('', array('class="td25"', 'class="td25"', '', '', '',''), array(
						in_array($nav['type'], array('2', '1')) ? "<input class=\"checkbox\" type=\"checkbox\" name=\"delete[]\" value=\"$nav[id]\">" : '',
						"<input type=\"text\" class=\"txt\" size=\"2\" name=\"displayordernew[$nav[id]]\" value=\"$nav[displayorder]\">",
						"<div><input type=\"text\" class=\"txt\" size=\"15\" name=\"namenew[$nav[id]]\" value=\"".dhtmlspecialchars($nav['name'])."\">".
							"<a href=\"###\" onclick=\"addrowdirect=1;addrow(this, 1, $nav[id])\" class=\"addchild\">$lang[misc_customnav_add_submenu]</a></div>",
						$nav['type'] == '0' || $nav['type'] == '4' ? "<span title='{$nav['url']}'>".$nav['url'].'<span>' : "<input type=\"text\" class=\"txt url\" size=\"15\" name=\"urlnew[$nav[id]]\" value=\"".dhtmlspecialchars($nav['url'])."\">",
						cplang($nav['type'] == '0' ? 'inbuilt' : ($nav['type'] == '4' ? 'channel' : 'custom')),
						"<input class=\"checkbox\" type=\"checkbox\" name=\"availablenew[$nav[id]]\" value=\"1\" ".($nav['available'] ? 'checked' : '').">",
						"<a href=\"".ADMINSCRIPT."?action=nav&operation=headernav&do=edit&id=$nav[id]\" class=\"act\">$lang[edit]</a>"
					));
					if($nav['sub']){
						$subnavnum = count($nav['sub']);
						foreach((array)$nav['sub'] as $sub) {
							$subnavnum--;
							showtablerow('', array('class="td25"', 'class="td25"', '', ''), array(
								$sub['type'] == '0' || $sub['type'] == '4' ? '' : "<input class=\"checkbox\" type=\"checkbox\" name=\"delete[]\" value=\"$sub[id]\">",
								"<input type=\"text\" class=\"txt\" size=\"2\" name=\"displayordernew[$sub[id]]\" value=\"$sub[displayorder]\">",
								"<div class=\"".($subnavnum ? 'board' : 'lastboard')."\"><input type=\"text\" class=\"txt\" size=\"15\" name=\"namenew[$sub[id]]\" value=\"".dhtmlspecialchars($sub['name'])."\">".
									"<a href=\"###\" onclick=\"addrowdirect=1;addrow(this, 2, $sub[id])\" class=\"addchild\">$lang[misc_customnav_add_submenu]</a></div>",
								$sub['type'] == '0' || $sub['type'] == '4' ? "<span title='{$sub['url']}'>".$sub['url'].'</span>' : "<input type=\"text\" class=\"txt url\" size=\"15\" name=\"urlnew[$sub[id]]\" value=\"".dhtmlspecialchars($sub['url'])."\">",
								cplang($sub['type'] == '0' ? 'inbuilt' : ($sub['type'] == '4' ? 'channel' : 'custom')),
								"<input class=\"checkbox\" type=\"checkbox\" name=\"availablenew[$sub[id]]\" value=\"1\" ".($sub['available'] ? 'checked' : '').">",
								"<a href=\"".ADMINSCRIPT."?action=nav&operation=headernav&do=edit&id=$sub[id]\" class=\"act\">$lang[edit]</a>"
							));
							if($sub['sub']){
								$thdnavnum = count($sub['sub']);
								foreach((array)$sub['sub'] as $thd) {
									$thdnavnum--;
									showtablerow('', array('class="td25"', 'class="td25"', '', ''), array(
										$thd['type'] == '0' || $thd['type'] == '4' ? '' : "<input class=\"checkbox\" type=\"checkbox\" name=\"delete[]\" value=\"$thd[id]\">",
										"<input type=\"text\" class=\"txt\" size=\"2\" name=\"displayordernew[$thd[id]]\" value=\"$thd[displayorder]\">",
										"<div class=\"".($thdnavnum ? 'childboard' : 'lastchildboard')."\"><input type=\"text\" class=\"txt\" size=\"15\" name=\"namenew[$thd[id]]\" value=\"".dhtmlspecialchars($thd['name'])."\"></div>",
										$thd['type'] == '0' || $thd['type'] == '4' ? "<span title='{$thd['url']}'>".$thd['url'].'</span>' : "<input type=\"text\" class=\"txt url\" size=\"15\" name=\"urlnew[$thd[id]]\" value=\"".dhtmlspecialchars($thd['url'])."\">",
										cplang($thd['type'] == '0' ? 'inbuilt' : ($thd['type'] == '4' ? 'channel' : 'custom')),
										"<input class=\"checkbox\" type=\"checkbox\" name=\"availablenew[$thd[id]]\" value=\"1\" ".($thd['available'] ? 'checked' : '').">",
										"<a href=\"".ADMINSCRIPT."?action=nav&operation=headernav&do=edit&id=$thd[id]\" class=\"act\">$lang[edit]</a>"
									));
								}
							}
						}
					}
				}
			}
			echo '<tr><td colspan="1"></td><td colspan="8"><div><a href="###" onclick="addrow(this, 0, 0)" class="addtr">'.$lang['misc_customnav_add_menu'].'</a></div></td></tr>';
			showsubmit('submit', 'submit', 'del');
			showtablefooter();
			showformfooter();

			echo <<<EOT
<script type="text/JavaScript">
	var rowtypedata = [
		[[1, '', 'td25'],
				[1,'<input name="newdisplayorder[]" value="" size="3" type="text" class="txt">', 'td25'],
				[1, '<input name="newname[]" value="" size="15" type="text" class="txt">'],
				[5, '<input name="newurl[]" value="" size="15" type="text" class="txt url"><input type="hidden" name="newparentid[]" value="0" />']],
		[[1, '', 'td25'],
				[1,'<input name="newdisplayorder[]" value="" size="3" type="text" class="txt">', 'td25'],
				[1, '<div class=\"board\"><input name="newname[]" value="" size="15" type="text" class="txt"></div>'],
				[5, '<input name="newurl[]" value="" size="15" type="text" class="txt url"><input type="hidden" name="newparentid[]" value="{1}" />']],
		[[1, '', 'td25'],
				[1,'<input name="newdisplayorder[]" value="" size="3" type="text" class="txt">', 'td25'],
				[1, '<div class=\"childboard\"><input name="newname[]" value="" size="15" type="text" class="txt"></div></div>'],
				[5, '<input name="newurl[]" value="" size="15" type="text" class="txt url"><input type="hidden" name="newparentid[]" value="{1}" />']]
	];
</script>
EOT;

		} else {

			if($ids = dimplode($_G['gp_delete'])) {
				DB::query("DELETE FROM #__common_nav WHERE navtype='0' AND id IN ($ids)");
				DB::query("DELETE FROM #__common_nav WHERE navtype='0' AND parentid IN ($ids)");
			}

			if(is_array($_G['gp_namenew'])) {
				foreach((array)$_G['gp_namenew'] as $id => $name) {
					$name = trim(dhtmlspecialchars($name));
					$urlnew = str_replace(array('&amp;'), array('&'), dhtmlspecialchars($_G['gp_urlnew'][$id]));
					$urladd = !empty($_G['gp_urlnew'][$id]) ? ", url='$urlnew'" : '';
					$availablenew[$id] = $name && (!isset($_G['gp_urlnew'][$id]) || $_G['gp_urlnew'][$id]) && $_G['gp_availablenew'][$id];
					$displayordernew[$id] = intval($_G['gp_displayordernew'][$id]);
					$nameadd = !empty($name) ? ", name='$name'" : '';
					DB::query("UPDATE #__common_nav SET displayorder='$displayordernew[$id]', available='$availablenew[$id]' $titleadd $urladd $nameadd WHERE id='$id'");
				}
			}

			if(is_array($_G['gp_newname'])) {
				foreach((array)$_G['gp_newname'] as $k => $v) {
					$v = dhtmlspecialchars(trim($v));
					if(!empty($v)) {
						$newavailable = $v && $_G['gp_newurl'][$k];
						$newparentid[$k] = intval($_G['gp_newparentid'][$k]);
						$newdisplayorder[$k] = intval($_G['gp_newdisplayorder'][$k]);
						$newurl[$k] = str_replace('&amp;', '&', dhtmlspecialchars($_G['gp_newurl'][$k]));
						$data = array(
							'parentid' => $newparentid[$k],
							'name' => $v,
							'displayorder' => $newdisplayorder[$k],
							'url' => $newurl[$k],
							'type' => 1,
							'available' => $newavailable,
							'navtype' => 0
						);
						//pre($data);
						DB::insert('common_nav', $data);
					}
				}
				//exit;
			}

			updatecache('setting');
			cpmsg('nav_add_succeed', 'action=nav&operation=headernav', 'succeed');

		}

	} elseif($do == 'edit' && ($id = $_G['gp_id'])) {

		$nav = DB::fetch_first("SELECT * FROM #__common_nav WHERE navtype='0' AND id='$id'");
		if(!$nav) {
			cpmsg('Undefined action, please return.', '', 'error');
		}

		if(!submitcheck('editsubmit')) {
			shownav('global', 'misc_customnav');
			showsubmenu('nav_setting_customnav', $navdata);
			$query = DB::query("SELECT * FROM #__common_nav WHERE navtype='0' AND parentid='0' ORDER BY displayorder");
			$parentselect = array(array('0', cplang('misc_customnav_parent_top')));
			$parentname = '';
			while($pnavs = DB::fetch($query)) {
				if($pnavs['id'] != $id) {
					$parentselect[] = array($pnavs['id'], '&nbsp;&nbsp;'.$pnavs['name']);
					if($nav['parentid'] == $pnavs['id']) {
						$parentname = ' - '.$pnavs['name'];
					}
				}
			}

			showformheader("nav&operation=headernav&do=edit&id=$id");
			showtableheader();
			showtitle(cplang('nav_nav_headernav').$parentname.' - '.$nav['name']);
			showsetting('misc_customnav_name', 'namenew', $nav['name'], 'text');
			showsetting('misc_customnav_parent', array('parentidnew', $parentselect), $nav['parentid'], 'select');
			showsetting('misc_customnav_title', 'titlenew', $nav['title'], 'text');
			showsetting('misc_customnav_url', 'urlnew', $nav['url'], 'text', ($nav['type'] == '0' || $nav['type'] == '4'));
			showsetting('misc_customnav_url_open', array('targetnew', array(
				array(0, cplang('misc_customnav_url_open_default')),
				array(1, cplang('misc_customnav_url_open_blank'))
			), TRUE), $nav['target'], 'mradio');
			showsubmit('editsubmit');
			showtablefooter();
			showformfooter();

		} else {

			$namenew = trim(dhtmlspecialchars($_G['gp_namenew']));
			$titlenew = trim(dhtmlspecialchars($_G['gp_titlenew']));
			$urlnew = str_replace(array('&amp;'), array('&'), dhtmlspecialchars($_G['gp_urlnew']));
			$parentidnew = $_G['gp_parentidnew'];
			$targetnew = intval($_G['gp_targetnew']) ? 1 : 0;
			$urladd = $nav['type'] != '0' && $urlnew ? ", url='".$urlnew."'" : '';

			DB::query("UPDATE #__common_nav SET name='$namenew', parentid='$parentidnew', title='$titlenew', target='$targetnew' $urladd WHERE id='$id'");

			updatecache('setting');
			cpmsg('nav_add_succeed', 'action=nav&operation=headernav', 'succeed');

		}

	}

} elseif($operation == 'footernav') {

	if(!$do) {

		if(!submitcheck('submit')) {

			shownav('style', 'nav_setting_customnav');
			showsubmenu('nav_setting_customnav', $navdata);

			showformheader('nav&operation=footernav');
			showtableheader();
			showsubtitle(array('', 'display_order', 'name', 'url', 'type', 'available', ''));

			$navlist = array();
			$query = DB::query("SELECT * FROM #__common_nav WHERE navtype='1' ORDER BY displayorder");
			while($nav = DB::fetch($query)) {
				$navlist[$nav['id']] = $nav;
			}

			foreach((array)$navlist as $nav) {
				showtablerow('', array('class="td25"', 'class="td25"', '', ''), array(
					in_array($nav['type'], array('2', '1')) ? "<input class=\"checkbox\" type=\"checkbox\" name=\"delete[]\" value=\"$nav[id]\">" : '',
					"<input type=\"text\" class=\"txt\" size=\"2\" name=\"displayordernew[$nav[id]]\" value=\"$nav[displayorder]\">",
					"<div><input type=\"text\" class=\"txt\" size=\"15\" name=\"namenew[$nav[id]]\" value=\"".dhtmlspecialchars($nav['name'])."\">",
					$nav['type'] == '0' ? $nav['url'] : "<input type=\"text\" class=\"txt\" size=\"15\" name=\"urlnew[$nav[id]]\" value=\"".dhtmlspecialchars($nav['url'])."\">",
					cplang($nav['type'] == '0' ? 'inbuilt' : ($nav['type'] == '4' ? 'channel' : 'custom')),
					"<input class=\"checkbox\" type=\"checkbox\" name=\"availablenew[$nav[id]]\" value=\"1\" ".($nav['available'] ? 'checked' : '').">",
					"<a href=\"".ADMINSCRIPT."?action=nav&operation=footernav&do=edit&id=$nav[id]\" class=\"act\">$lang[edit]</a>"
				));
			}
			echo '<tr><td colspan="1"></td><td colspan="7"><div><a href="###" onclick="addrow(this, 0, 0)" class="addtr">'.$lang['nav_footernav_add'].'</a></div></td></tr>';
			showsubmit('submit', 'submit', 'del');
			showtablefooter();
			showformfooter();

			echo <<<EOT
<script type="text/JavaScript">
	var rowtypedata = [
		[[1, '', 'td25'],
				[1,'<input name="newdisplayorder[]" value="" size="3" type="text" class="txt">', 'td25'],
				[1, '<input name="newname[]" value="" size="15" type="text" class="txt">'],
				[4, '<input name="newurl[]" value="" size="15" type="text" class="txt url">']],
	];
</script>
EOT;

		} else {

			if($ids = dimplode($_G['gp_delete'])) {
				DB::query("DELETE FROM #__common_nav WHERE navtype='1' AND id IN ($ids)");
			}

			if(is_array($_G['gp_namenew'])) {
				foreach((array)$_G['gp_namenew'] as $id => $name) {
					$name = trim(dhtmlspecialchars($name));
					$urlnew = str_replace(array('&amp;'), array('&'), dhtmlspecialchars($_G['gp_urlnew'][$id]));
					$urladd = !empty($_G['gp_urlnew'][$id]) ? ", url='$urlnew'" : '';
					$availablenew[$id] = $name && (!isset($_G['gp_urlnew'][$id]) || $_G['gp_urlnew'][$id]) && $_G['gp_availablenew'][$id];
					$displayordernew[$id] = intval($_G['gp_displayordernew'][$id]);
					$nameadd = !empty($name) ? ", name='$name'" : '';
					DB::query("UPDATE #__common_nav SET displayorder='$displayordernew[$id]', available='$availablenew[$id]' $titleadd $urladd $nameadd WHERE id='$id'");
				}
			}

			if(is_array($_G['gp_newname'])) {
				foreach((array)$_G['gp_newname'] as $k => $v) {
					$v = dhtmlspecialchars(trim($v));
					if(!empty($v)) {
						$newavailable = $v && $_G['gp_newurl'][$k];
						$newdisplayorder[$k] = intval($_G['gp_newdisplayorder'][$k]);
						$newurl[$k] = str_replace('&amp;', '&', dhtmlspecialchars($_G['gp_newurl'][$k]));
						$data = array(
							'name' => $v,
							'displayorder' => $newdisplayorder[$k],
							'url' => $newurl[$k],
							'type' => 1,
							'available' => $newavailable,
							'navtype' => 1
						);
						DB::insert('common_nav', $data);
					}
				}
			}

			updatecache('setting');
			cpmsg('nav_add_succeed', 'action=nav&operation=footernav', 'succeed');

		}

	} elseif($do == 'edit' && ($id = $_G['gp_id'])) {

		$nav = DB::fetch_first("SELECT * FROM #__common_nav WHERE navtype='1' AND id='$id'");
		if(!$nav) {
			cpmsg('Undefined action, please return.', '', 'error');
		}

		if(!submitcheck('editsubmit')) {
			shownav('global', 'misc_customnav');
			showsubmenu('nav_setting_customnav', $navdata);

			showformheader("nav&operation=footernav&do=edit&id=$id");
			showtableheader();
			showtitle(cplang('nav_nav_footernav').' - '.$nav['name']);
			showsetting('misc_customnav_name', 'namenew', $nav['name'], 'text');
			showsetting('misc_customnav_title', 'titlenew', $nav['title'], 'text');
			showsetting('misc_customnav_url', 'urlnew', $nav['url'], 'text', $nav['type'] == '0');
			showsetting('misc_customnav_url_open', array('targetnew', array(
				array(0, cplang('misc_customnav_url_open_default')),
				array(1, cplang('misc_customnav_url_open_blank'))
			), TRUE), $nav['target'], 'mradio');
			showtagfooter('tbody');
			showsubmit('editsubmit');
			showtablefooter();
			showformfooter();

		} else {

			$namenew = trim(dhtmlspecialchars($_G['gp_namenew']));
			$titlenew = trim(dhtmlspecialchars($_G['gp_titlenew']));
			$urlnew = str_replace(array('&amp;'), array('&'), dhtmlspecialchars($_G['gp_urlnew']));
			$targetnew = intval($_G['gp_targetnew']) ? 1 : 0;
			$urladd = $nav['type'] != '0' && $urlnew ? ", url='".$urlnew."'" : '';

			DB::query("UPDATE #__common_nav SET name='$namenew', parentid='$parentidnew', title='$titlenew', target='$targetnew' $urladd WHERE id='$id'");

			updatecache('setting');
			cpmsg('nav_add_succeed', 'action=nav&operation=footernav', 'succeed');

		}

	}


}

?>